﻿using AutoMapper;
using PagedList;
using SIG.DAL.Dapper.Model;
using SIG.DAL.Dapper.Repositories;
using SIG.Infrastructure.Configs;
using SIG.ViewModel.Front.Works;
using System.Linq;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public class WorkController : BaseController
    {
        private readonly WorkCategoryRepository _categoryService;
        private readonly WorkRepository _workService;
        // private readonly IWorkPhotoService _workPhotoService;
        private readonly PageMetaRepository _pageMetaService;
        private readonly IMapper _mapper;

        public WorkController(IMapper mapper)
        {
            _categoryService = new WorkCategoryRepository();
            _workService = new WorkRepository();
            //_workPhotoService = workPhotoService;
            _pageMetaService = new PageMetaRepository();
            _mapper = mapper;

        }

        [Route("work/{seoName?}")]
        // GET: Work
        public ActionResult Index(int? page, string seoName)
        {
            // List<Work> works = new List<Work>();

            var vm = new WorkListVM()
            {
                SeoName = seoName,
                PageIndex = page ?? 1,
                PageSize = SettingsManager.Case.FrontPageSize
            };

            if (!string.IsNullOrEmpty(seoName))
            {
                var category = _categoryService.GetItemBySeoName(seoName);
                if (category == null)
                    return HttpNotFound();

                int count;
                var works = _workService.GetListPagedAcitveItems(vm.PageIndex, vm.PageSize, category.Id, string.Empty, out count);
                vm.TotalCount = count;
                // var list = _mapper.Map<List<Work>, List<WorkVM>>(works);
                // var returnList = list.Skip(vm.PageIndex).Take(vm.PageSize);
                vm.Works = new StaticPagedList<SIG.DAL.Dapper.Model.Work>(works, vm.PageIndex, vm.PageSize, vm.TotalCount);

            }
            else
            {
                int totalCount = 0;
                var list = _workService.GetListPagedAcitveItems(vm.PageIndex, vm.PageSize, 0, string.Empty, out totalCount);
                vm.TotalCount = totalCount;
                //  var dto = _mapper.Map<List<Work>, List<WorkVM>>(list);
                vm.Works = new StaticPagedList<SIG.DAL.Dapper.Model.Work>(list, vm.PageIndex, vm.PageSize, vm.TotalCount);

            }

            vm.Categories = _categoryService.GetActiveItems().OrderByDescending(m => m.Importance);
            // vm.Categories = _mapper.Map<List<Category>, List<CategoryVM>>(categoryList);

            var url = Request.RawUrl;
            vm.PageMeta = _pageMetaService.GetPageMeta(ModelType.MENU, url);

            return View(vm);

        }

        public ActionResult WorkCategories()
        {
            var vm = _categoryService.GetActiveItems().OrderByDescending(m => m.Importance);
            return PartialView("_WorkCategories", vm);
        }
        [Route("about/business")]
        public ActionResult Business()
        {

            BusinessVM vm = new BusinessVM();
            vm.Categories = _categoryService.GetActiveItems().OrderByDescending(m => m.Importance);

            var url = Request.RawUrl;
            vm.PageMeta = _pageMetaService.GetPageMeta(ModelType.MENU, url);

            return View(vm);
        }


        public ActionResult Detail(int id)
        {
            Work vWork = _workService.GetById(id);            
            if (vWork == null)
            {
                return HttpNotFound();
            }

            vWork.ViewCount += 1;
            _workService.Update(vWork);

            // var vm = _mapper.Map<ProductDetail>(product);
            WorkDetail vm = new WorkDetail
            {
                Id = vWork.Id,
                Title = vWork.Title,
              
                Body = vWork.Body,
               // Description = vWork.Description,
                ViewCount = vWork.ViewCount
            };

            if (!string.IsNullOrEmpty(vWork.ImageUrl))
            {
                vm.Photos = vWork.ImageUrl.Split('|');
            }



            var pageMeta = _pageMetaService.GetPageMeta(ModelType.WORK, vWork.Id.ToString());
            if (pageMeta != null)
            {
                vm.SEOTitle = pageMeta.Title;
                vm.Keywords = pageMeta.Keyword;
                vm.SEODescription = pageMeta.Description;
            }


            return View(vm);
        }

      
    }
}